﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace KyCommonExtend
{
    /// <summary>
    /// 扩展库
    /// </summary>
    public static class KyObjectExtend
    {

        /// <summary>
        /// 对象转为字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToNullString(this object obj, string defaultString = "")
        {
            if (obj != null || obj == DBNull.Value)
            {
                string val = obj.ToString();
                if (val == null) return string.Empty;
                return val.ToString();
            }
            return defaultString;
        }

        /// <summary>
        /// 对象转Long
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultreturn">默认返回值</param>
        /// <returns></returns>
        public static long ToLong(this object obj, long defaultreturn = 0)
        {
            string nullString = obj.ToNullString();
            if (string.IsNullOrWhiteSpace(nullString)) return defaultreturn;

            if (long.TryParse(nullString, out long result))
            {
                return result;
            }
            return defaultreturn;
        }

        /// <summary>
        /// 对象转Double
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultreturn">默认返回值</param>
        /// <returns></returns>
        public static double ToDouble(this object obj, double defaultreturn = 0.0)
        {
            string nullString = obj.ToNullString();
            if (string.IsNullOrWhiteSpace(nullString)) return defaultreturn;
            if (double.TryParse(nullString, out double result))
            {
                return result;
            }
            return defaultreturn;
        }

        /// <summary>
        /// 对象转Decimal
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultreturn">默认返回值</param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj, decimal defaultreturn = 0)
        {
            string nullString = obj.ToNullString();
            if (string.IsNullOrWhiteSpace(nullString)) return defaultreturn;

            if (decimal.TryParse(nullString, out decimal result))
            {
                return result;
            }
            return defaultreturn;
        }
        /// <summary>
        /// 对象转float
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultreturn">默认返回值</param>
        /// <returns></returns>
        public static float ToFloat(this object obj, float defaultreturn = 0.0f)
        {
            string nullString = obj.ToNullString();
            if (string.IsNullOrWhiteSpace(nullString)) return defaultreturn;
            float result = defaultreturn;
            float.TryParse(nullString, out result);
            return result;
        }

        /// <summary>
        ///  对象转DateTime
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object obj)
        {
            string nullString = obj.ToNullString();
            if (DateTime.TryParse(nullString, out DateTime result))
            {
                if (result == DateTime.MinValue) return new DateTime();
                return result;
            }
            return new DateTime();
        }



        /// <summary>
        /// 获取本周一的日期
        /// </summary>
        /// <returns>返回格式为"2022-01-01"</returns>
        public static DateTime GetMonday(this DateTime time)
        {
            DateTime temp = new DateTime(time.Year, time.Month, time.Day);
            int count = time.DayOfWeek - DayOfWeek.Monday;
            if (count == -1) count = 6;
            return temp.AddDays(-count).Date;
        }


        /// <summary>
        /// DateTime转时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long ToTimeStamp(this DateTime time)
        {
            return Convert.ToInt64((time - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
        }


        /// <summary>
        /// 获取当前日期是一年第几周
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>

        public static int WeekOfYear(this DateTime date)
        {
            // 参数说明：
            // CalendarWeekRule.FirstDay - 第一周需包含 1月1日
            // DayOfWeek.Monday - 周起始日为周一（可改为 Sunday 等）
            var gc = new GregorianCalendar();
            return gc.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
        }

    }
}
